﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using RapidWebDev.Common;

namespace RapidWebDev.SearchIndex.Contract
{
	/// <summary>
	/// Search result message.
	/// </summary>
	[Serializable]
	[DataContract(Namespace = ServiceNamespaces.SearchIndexNamespace)]
	public class SearchResultDocMessage
	{
		/// <summary>
		/// Document id.
		/// </summary>
		[DataMember]
		public string Id { get; set; }

		/// <summary>
		/// Document type.
		/// </summary>
		[DataMember]
		public string Type { get; set; }

		/// <summary>
		/// Document highlight fragment which only works when SearchParameterMessage.Selector.ReturnKeywordHighlightFragment equals to True.
		/// </summary>
		[DataMember]
		public string HighlightFragment { get; set; }

		/// <summary>
		/// Document fields.
		/// </summary>
		[DataMember]
		public Collection<SearchResultDocFieldMessage> Fields { get; set; }

		/// <summary>
		/// Index document attachment files.
		/// </summary>
		[DataMember]
		public Collection<SearchResultDocAttachmentMessage> Attachments { get; set; }

		/// <summary>
		/// The datetime when the document message is indexed on.
		/// </summary>
		[DataMember]
		public DateTime IndexedOn { get; set; }

		/// <summary>
		/// Get first or default field value.
		/// </summary>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public TFieldValueType FieldValue<TFieldValueType>(string fieldName)
		{
			if (this.Fields == null) return default(TFieldValueType);
			var searchDocField = this.Fields.FirstOrDefault(f => f.Name == fieldName);
			if (searchDocField == null) return default(TFieldValueType);
			if (searchDocField.Values.Count == 0) return default(TFieldValueType);

			string fieldValue = searchDocField.Values[0];
			return (TFieldValueType)Kit.ConvertType(fieldValue, typeof(TFieldValueType));
		}

		/// <summary>
		/// Convert the instance to string.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return string.Format(CultureInfo.InvariantCulture, "Type: {0}, Id: {1}", this.Type, this.Id);
		}
	}
}